package com.ricky;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by bgt on 2018/5/1.
 */
@SpringBootApplication
public class SpringBootStart02 {

    @Bean
    public Runnable createRun(){
        return ()->{
            System.out.println("我执行了呢");
        };
    }

    /**
     * 只有在profile.active是test的时候才装载
     * @return
     */
    @Bean
    @Profile("test")
    public Runnable createRun2(){
        return ()->{
            System.out.println("我执行了呢");
        };
    }

    /**
     *      * 只有在profile.active是dev的时候才装载
     * @return
     */
    @Bean
    @Profile("dev")
    public Runnable createRun3(){
        return ()->{
            System.out.println("我执行了呢");
        };
    }

    public static void main(String[] args) {
        SpringApplication app=new SpringApplication();
        Set<Class<?>> set=new HashSet<>();
        set.add(SpringBootStart02.class);
        app.addPrimarySources(set);
        //第一种设置profile
        app.setAdditionalProfiles("dev","test");
        //第二种设置 --spring.profile.active=test,dev
        ConfigurableApplicationContext run = app.run(args);
        run.getBean(Runnable.class).run();
        System.out.println("测试profile注入："+ run.getEnvironment().getProperty("rickyip2"));
//        context.close();
    }
}
